package com.zsh.wx_account1.util;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;

import java.util.Map;

/**
 * 关于微信授权登录
 * @Author ZhaoShuHao
 * @Date 2023/9/1 13:48
 */
//微信授权登录，第二步，根据第一步授权后返回的code获取access_token(code可以从重定向的路径中获取)
public class AccessTokenUtil {
    private static final String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

    //自己的公众号信息
    //测试号
    private static final String APP_ID = "wx43f47bd394a23feb";
    private static final String APP_SECRET = "1a29d7b96b7403c99b20d563057a10c8";
    //code一般只有5分钟的过期时间，过期后需要重新获取
    private static final String CODE="071Iy4ml2pjvXb4ujbll2Eg5644Iy4mb";
    public static void main(String[] args) {
        //1、根据code获取信息
      /*  Map accessToken = getAccessToken();
        System.out.println("accessToken："+accessToken);*/
        //2、根据refreshToken刷新token
     /*   String s = refreshTokenTest();
        System.out.println(s);*/
        //3、拉去用户信息
//        String s = pullUserInfo();
        //微信授权获取用户信息的完整流程
        weChatAuthorization();
    }
    /*1、根据code获取的数据
    {
	"access_token": "72_hts4ZVuBZJKc2vROr84IZLudWXkBPrn_1pE44FgTWo_UYxu-AwzWdaI81RMulEDXrYwuOe2Src0FH4UVo_YoArFBvWW8rQEu_O5tDbUaJBs",
	"expires_in": 7200,
	  "refresh_token": "72_ESvv83WfSTTPr4MAUH4ecI-2RWRhASkWgKqyCeWWLBABWIhxWUr-_R7E1i5Yxs4CFwEf6Y44fUi0UyPinV0xdDnnXsJKe_2xYabiNyDd7W0",
	"openid": "okuVs5iLGbRWwjjotu8EsKVURyDM",
	"scope": "snsapi_userinfo"
    }
    * */
    public static Map<String,Object> getAccessToken(){
        String formatUrl = String.format(url, APP_ID, APP_SECRET, CODE);
        System.out.println("formatUrl："+formatUrl);
        String s = HttpUtil.doGet(formatUrl);
        Map<String,Object> map = JSON.parseObject(s, Map.class);
        return map;
    }

    /*
    {
	"openid": "okuVs5iLGbRWwjjotu8EsKVURyDM",
	"access_token": "72_DmkvFgDsiySmI22VMVn82qlzlZ_tQfdu5sns-81ALUFXMK7StU9fRgxKRFQaQuW_QIgC2oGGF9jSAAowb3TVBW1jSPpCZSJgFAsmjXxQ6wc",
	"expires_in": 7200,
	"refresh_token": "72_VXoP27NOxJxILa1-MRT_2N-8Tt6mUwOrycLGjGt61j1UicZx3uwcVONRrpJdDXJvvUmSOfuiZ0iI5DD6HBB0LbgmUMEqJSMEsb7VXQXifNE",
	"scope": "snsapi_userinfo"
    }
    * */
    //2、（可选）因为accessToken的过期时间较短，可以使用refresh_token去刷新accessToken
    public static String refreshToken(String refreshToken){
        String format = String.format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s", APP_ID, refreshToken);
        String s = HttpUtil.doGet(format);
        System.out.println("tokenInfo："+s);
        return s;
    }
    //因为accessToken的过期时间较短，可以使用refresh_token去刷新accessToken
    public static String refreshTokenTest(){
        String format = String.format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s", APP_ID, "72_VXoP27NOxJxILa1-MRT_2N-8Tt6mUwOrycLGjGt61j1UicZx3uwcVONRrpJdDXJvvUmSOfuiZ0iI5DD6HBB0LbgmUMEqJSMEsb7VXQXifNE");
        String s = HttpUtil.doGet(format);
        System.out.println("tokenInfo："+s);
        return s;
    }

    /*
    {
	"openid": "okuVs5iLGbRWwjjotu8EsKVURyDM",
	"nickname": "要开心吖",
	"sex": 0,
	"language": "",
	"city": "",
	"province": "",
	"country": "",
	"headimgurl": "https:\/\/thirdwx.qlogo.cn\/mmopen\/vi_32\/DYAIOgq83erD0Q9Iv5YFf31GpZG36Mjmtcee8VyOxmnrlXn1hXH61P3DZmENrrpBalGP5WJ0Qe2KW6g7bPopiaQ\/132",
	"privilege": []
}
    * */
    //3、拉取用户信息
    public static String pullUserInfo(){
        String accessToken="72_DmkvFgDsiySmI22VMVn82qlzlZ_tQfdu5sns-81ALUFXMK7StU9fRgxKRFQaQuW_QIgC2oGGF9jSAAowb3TVBW1jSPpCZSJgFAsmjXxQ6wc";
        String openId="okuVs5iLGbRWwjjotu8EsKVURyDM";
        String format = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openId);
        String s = HttpUtil.doGet(format);
        System.out.println(s);
        return s;
    }

    //微信授权的完整流程
    public static String weChatAuthorization(){
        String formatUrl = String.format(url, APP_ID, APP_SECRET, CODE);
        System.out.println("formatUrl："+formatUrl);
        String tokeninfo = HttpUtil.doGet(formatUrl);
        Map<String,Object> map = JSON.parseObject(tokeninfo, Map.class);
        String accessToken= (String) map.get("access_token");
        String openId= (String) map.get("openid");
        String format = String.format("https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN", accessToken, openId);
        String userInfo = HttpUtil.doGet(format);
        System.out.println("tokeninfo："+tokeninfo);
        System.out.println("userInfo："+userInfo);
        return userInfo;
    }


}
